package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.dept.api.SysDepartmentService;
import com.nidu.demo.dept.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 部门管理 Controller
 */
@Tag(name = "管理后台 - 部门")
@RestController
@RequestMapping("/system/dept")
@Validated
@RequiredArgsConstructor
public class SysDepartmentController {

    private final SysDepartmentService service;

    @Operation(summary = "创建部门", description = "根据部门创建命令添加新的部门")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:dept:create')")
    public SingleResponse<DepartmentCO> addDept(@RequestBody DepartmentAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新部门", description = "根据部门更新命令更新指定的部门信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dept:update')")
    public SingleResponse<DepartmentCO> updateDept(@PathVariable Long id, @RequestBody DepartmentUpdateCmd cmd) {
        cmd.setId(id);  // 设置更新的部门ID
        return service.update(cmd);
    }

    @Operation(summary = "删除部门", description = "根据部门删除命令删除指定的部门")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    public Response deleteDept(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除部门", description = "根据部门删除命令删除指定的部门")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    public Response deleteDepts(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取部门详情", description = "根据部门ID获取部门的详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public SingleResponse<DepartmentCO> getDeptById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "查询部门列表", description = "根据查询条件获取部门列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public MultiResponse<DepartmentCO> listDepts(DepartmentListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "查询部门树", description = "根据部门树查询条件获取部门树结果，以公司为根节点")
    @GetMapping("/tree")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public MultiResponse<DepartmentTreeCO> treeDepts(DepartmentTreeQry qry) {
        return service.tree(qry);
    }

    @Operation(summary = "分页查询部门树", description = "分页查询部门树信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public PageResponse<DepartmentTreeCO> pageDepts(DepartmentPageQry qry) {
        return service.page(qry);
    }

    @Operation(summary = "更新部门状态", description = "更新部门的启用/禁用状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPermission('system:dept:update')")
    public Response updateStatus(@PathVariable Long id, @RequestParam StatusEnum status) {
        return service.updateStatus(id, status);
    }
}